using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;

namespace ITAuditBL
{
    public class AuditBL
    {
        public static DomainCollection ShowMyDomains() {
            return Forest.GetCurrentForest().Domains;
        }

        public static DirectoryEntries ShowComputersByDomain(Domain domain) {
            DirectoryEntries computers = domain.GetDirectoryEntry().Children;
            computers.SchemaFilter.Add("computers");
            return computers;
        }

        public static DirectoryEntry SearchComputer(string name)
        {
            Domain domain = Domain.GetCurrentDomain();
            DirectoryEntry entry = domain.GetDirectoryEntry();
            DirectorySearcher mySearcher = new DirectorySearcher(entry);          
            mySearcher.Filter = "(&(objectClass=computer)(|(cn=" + name + ")(dn=" + name + ")))";
            SearchResult result = mySearcher.FindOne();
            if (result != null) return new DirectoryEntry(result.Path);
            return null;
        }

        public static DirectoryEntry SearchComputer(Domain domain,string name)
        {
            DirectoryEntry entry = domain.GetDirectoryEntry();
            DirectorySearcher mySearcher = new DirectorySearcher(entry);
            mySearcher.Filter = "(&(objectClass=computer)(|(cn=" + name + ")(dn=" + name + ")))";
            SearchResult result = mySearcher.FindOne();
            if (result != null) return new DirectoryEntry(result.Path);
            return null;
        }
    }
}
